package com.yuxl.centent.document.repository;

import com.yuxl.centent.document.entity.ContentDocument;
import com.yuxl.common.core.annotation.PostgreSqlRepository;
import org.reactivestreams.Publisher;
import org.springframework.data.r2dbc.repository.Query;
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * @Author : yuxl
 * @create 2025/9/8 下午7:58
 */
@PostgreSqlRepository
public interface ContentDocumentRepository extends ReactiveCrudRepository<ContentDocument, Long> {
    @Query("""
            SELECT count(*) FROM content_document
            WHERE 1 = 1
            AND (:#{#entity.title} IS NULL OR title LIKE CONCAT('%', :#{#entity.title}, '%'))
            """)
    Mono<Integer> count(ContentDocument entity);

    @Query("""
            SELECT * FROM content_document
            WHERE 1=1
            AND (:#{#entity.title} IS NULL OR title LIKE CONCAT('%', :#{#entity.title}, '%'))
            LIMIT :limit OFFSET :offset
            """)
    Flux<ContentDocument> page(ContentDocument entity, long limit, long offset);

    @Query("""
            SELECT * FROM content_document
            WHERE 1=1
            AND (:#{#entity.title} IS NULL OR title LIKE CONCAT('%', :#{#entity.title}, '%'))
            """)
    Flux<ContentDocument> list(ContentDocument entity);

    @Query("""
            SELECT id,path FROM content_document
            """)
    Flux<ContentDocument> pathAll();


    // 添加根据id 的 update语句
    @Query("""
            UPDATE content_document
            SET title = :#{#entity.title},
                path = :#{#entity.path},
                description = :#{#entity.description},
                update_time = :#{#entity.updateTime}
            WHERE id = :#{#entity.id}
            RETURNING *
            """)
    Mono<ContentDocument> updateById(ContentDocument entity);

    @Query("""
            UPDATE content_document
            SET text = :#{#entity.text}
            WHERE id = :#{#entity.id}
            RETURNING text
            """)
    Mono<ContentDocument> updateTextById(ContentDocument entity);


}
